/**
 * 2009/12/05
 */
package com.googlecode.felicalib.sample.polling;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.felicalib.sample.Felica;
import com.googlecode.felicalib.sample.exception.FelicaPollingException;

/**
 * @author o.tanaka
 */
public class FelicaPollingHostThread extends Thread {
    /**
     * ロギングインスタンス.
     */
    private static final Log LOG =
        LogFactory.getLog(FelicaPollingHostThread.class);
    /**
     * Felica felica
     */
    private final Felica felica;
    /**
     * int systemCode
     */
    private final int systemCode;

    /**
     * Polling polling
     */
    private final Polling polling;

    private boolean runLevel = true;

    /**
     * @param aFelica
     *            Felica
     * @param aSystemCode
     *            SystemCode
     * @param aPolling
     *            Polling
     */
    public FelicaPollingHostThread(
            final Felica aFelica,
            final int aSystemCode,
            final Polling aPolling) {
        this.felica = aFelica;
        this.systemCode = aSystemCode;
        this.polling = aPolling;
    }

    /**
     * スレッドの停止.
     */
    public void end() {
        this.runLevel = false;
    }

    /**
     * @param aServiceCode
     *            serviceCode
     * @param anAddr
     *            addr
     */
    public void getData(final int aServiceCode, final byte anAddr) {
        final CardInfo cardInfo = new CardInfo();
        cardInfo.setServiceCode(aServiceCode);
        cardInfo.setAddr(anAddr);
        try {
            cardInfo.setValue(this.felica.read(
                    this.systemCode,
                    aServiceCode,
                    anAddr));
        } catch (final FelicaPollingException e) {
            cardInfo.setValue(null);
        }
        this.polling.putData(cardInfo);

    }

    /**
     */
    public void getIDm() {
        try {
            this.polling.setIDm(
                    this.felica.getID(
                    this.systemCode));
        } catch (final FelicaPollingException e) {
            this.polling.setIDm(null);
        }
    }

    /**
     */
    public void getPMm() {
        try {
            this.polling.setPMm(
                    this.felica.getPM(
                    this.systemCode));
        } catch (final FelicaPollingException e) {
            this.polling.setPMm(null);
        }
    }

    /**
     * @return polling
     */
    public final Polling getPolling() {
        return this.polling;
    }

    /**
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        while (this.runLevel) {
            this.sleep(500);
        }
    }

    /**
     * @param aRunLevel
     *            the runLevel to set
     */
    public final void setRunLevel(final boolean aRunLevel) {
        this.runLevel = aRunLevel;
    }

    /**
     *
     */
    private void sleep(final int time) {
        try {
            Thread.sleep(time); // 0.5秒おきに読み取り
        } catch (final InterruptedException e) {
            FelicaPollingHostThread.LOG.error("sleep:", e);
        }
    }

    /**
     * @param aServiceCode
     *            serviceCode
     * @param aAddr
     *            addr
     * @param aData
     *            data
     * @return boolean
     */
    public boolean write(
            final int aServiceCode,
            final byte aAddr,
            final byte[] aData) {
        int i = -1;
        try {
            i = this.felica.write(this.systemCode, aServiceCode, aAddr, aData);
        } catch (final FelicaPollingException e) {
            FelicaPollingHostThread.LOG.error("write:", e);
        }
        if (i == 0) {
            return true;
        }
        return false;
    }
}
